<?php
namespace app\admin\validate;

use think\Validate;

class serviceType extends Validate 
{
    protected $rule = [
        'name' => 'require|checTypeName',
        'dept_id' => 'require'
    ];
    
    protected $message = [
        'name.require' => '名称不能为空！',
        'dept_id.require' => '主管部门不能为空！',
        'name.checTypeName' => '类型名称已存在！'
    ];
    
    public function checTypeName($value, $rule, $data) 
    {
        $where = [
            'name' => $value,
            'dept_id' => $data['dept_id'],
            'isvalid' => 1
        ];
        if (isset($data['id'])) {
            $where['id'] = ['neq', $data['id']];
        }
        $lists = db('service_type')->where($where)->find();
        
        return $lists ? false : true;
    }
}
